package cn.szzsi._admin;

import cn.szzsi.common.bean.SystemConfig;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Friendlink;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 商户后台类 - 友情链接 2016/9/26.
 */
public class FriendLinkController extends BaseAdminController<Friendlink> {
    private Friendlink friendlink;
    private File logo;
    private String logoFileName;
    private String shopId;

    // 添加
    public void add() {
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        render("friend_link_input.html");
    }

    // 编辑
    // 定位到编辑页面
    public void edit() {
        shopId=getAttr("shopId");
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("friendlink", Friendlink.dao.findById(id));
        }
        setAttr("shopId",shopId);
        render("friend_link_input.html");
    }

    // 列表
    public void list() {
        shopId=getAttr("shopId");
        findBusinessByPage(shopId,"mall_friendlink");
        render("friend_link_list.html");
    }
    //ajax list
    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<Friendlink> lfl = Friendlink.dao.find("select * from mall_friendlink where uid = ? order by createDate desc",shopId);
        renderJson(lfl);
    }


    // 保存
    //@Before(BusinessFriendLinkValidator.class)
    public void save() throws IOException {
        shopId=getAttr("shopId");
        friendlink = getModel(Friendlink.class);
        friendlink.set("uid",shopId);

        String allowedUploadImageExtension = getSystemConfig().getAllowedUploadImageExtension().toLowerCase();
        String[] imageExtensionArray = allowedUploadImageExtension.split(SystemConfig.EXTENSION_SEPARATOR);
        if (logo != null) {
            String logoExtension = StringUtils.substringAfterLast(logoFileName, ".").toLowerCase();
            if (!ArrayUtils.contains(imageExtensionArray, logoExtension)) {
                addActionError("只允许上传图片文件类型: " + allowedUploadImageExtension + "!");
                return;
            }
        }
        int uploadLimit = getSystemConfig().getUploadLimit() * 1024;
        if (uploadLimit != 0) {
            if (logo != null && logo.length() > uploadLimit) {
                addActionError("Logo文件大小超出限制!");
                return;
            }
        }
        if (logo != null){
            String logoFilePath = SystemConfig.UPLOAD_IMAGE_DIR + CommonUtil.getUUID() + "." + StringUtils.substringAfterLast(logoFileName, ".").toLowerCase();
            File logoFile = new File(PathKit.getWebRootPath() + logoFilePath);
            FileUtils.copyFile(logo, logoFile);
            /*FileUtils.moveFile(logo, logoFile);*/
            friendlink.set("logo", logoFilePath);
        }
        saved(friendlink);
        renderJson(Ret.ok("info","添加成功").set("url","list"));
        //redirect("/shop/friendLink/list");
    }

    // 更新
    //@Before(BusinessFriendLinkValidator.class)
    public void update() throws IOException {
        friendlink = getModel(Friendlink.class);
        String allowedUploadImageExtension = getSystemConfig().getAllowedUploadImageExtension().toLowerCase();
        String[] imageExtensionArray = allowedUploadImageExtension.split(SystemConfig.EXTENSION_SEPARATOR);
        if (logo != null) {
            String logoExtension = StringUtils.substringAfterLast(logoFileName, ".").toLowerCase();
            if (!ArrayUtils.contains(imageExtensionArray, logoExtension)) {
                addActionError("只允许上传图片文件类型: " + allowedUploadImageExtension + "!");
                return;
            }
        }
        int uploadLimit = getSystemConfig().getUploadLimit() * 1024;
        if (uploadLimit != 0) {
            if (logo != null && logo.length() > uploadLimit) {
                addActionError("Logo文件大小超出限制!");
                return;
            }
        }
        if (logo != null) {
            String logoFilePath = null;
            if (friendlink.getStr("logo") != null) {
                logoFilePath = friendlink.getStr("logo");
            } else {
                logoFilePath = SystemConfig.UPLOAD_IMAGE_DIR + CommonUtil.getUUID() + "." + StringUtils.substringAfterLast(logoFileName, ".").toLowerCase();
            }
            File logoFile = new File(PathKit.getWebRootPath() + logoFilePath);
            FileUtils.copyFile(logo, logoFile);
            friendlink.set("logo",logoFilePath);
        }
        updated(friendlink);
        renderJson(Ret.ok("info","修改成功").set("url","list"));
    }

    // 删除
    public void delete(){
        String id=getPara("id");
        if(Friendlink.dao.deleteById(id)){
            renderJson(Ret.ok());
        }else{
            renderJson(Ret.fail());

        }
    }
}
